# 添加采购数量要求
products  = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
myproduct = dict(products)
shop_car  = []
shop_cost = []
shopping_list = myproduct.keys()
shop_index = dict(enumerate(shopping_list))
#print(shop_index)
print("你可以采购下面的产品")
print("编号   产品           单价")
print('*'* 30)
for index,name in enumerate(shopping_list):
    print("{}:\t{}\t\t{}".format(index,name,myproduct[name]))
while True:
    if  shop_cost:
        print(shop_cost)

    n = input("请输入数字加入购物车或者按Ｑ键结束:  ").strip()
    if n.isdigit():
        num = int(n)
        if num not in shop_index:
            print("系统无法识别你的产品编号或者产品不存在，\033[31m请重新输入\033[0m")
            continue
        else:
            qty = input("请输入采购产品：{}的数量:  ".format(shop_index[num])).strip()
            if qty.isdigit() and int(qty) > 0:
                shop_car.append(shop_index[num])
                shop_cost.append((shop_index[num],int(qty)))
                continue
                                
            else:
                print("输入数量不对，请重新开始：。。。")
                continue
    elif n.upper() == "Q":
        if not shop_car:
            print("你没有采购产品，系统准备退出。。。")
            break
        else:
            print("准备结算购物清单")
            print("购物清单如下：")
            print("现有的购物如下:",shop_cost)
            for i,v in shop_cost:
#                 print(i)
#                 print(v)
#                 print(myproduct[i])
                print("{}   {}    {}".format(i,myproduct[i],v))
            myprice = 0 
            for i,v in shop_cost:
                myprice += myproduct[i] * v
            print("总价是:{}".format(myprice))
            break
    else:
        print("系统无法识别你的输入，请输入数字或者字母q")
        continue 
    
# 思考下字典和列表存储会不会更方便一些？